import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

class TestGameState extends GameState
{
	private ArrayList<Entity> ents;
	private Player player;
	
	private boolean c;
	
	public TestGameState(){}
	
	public String getName()
	{
		return "Test";
	}
	
	public void init()
	{
		ents = new ArrayList<Entity>();
		
		ents.add(new TestEntity(50,50));
		
		player = new Player(100,100);
		
		ents.add(player);
		
		display = new JPanel();
		keyListen = new KeyListen(this);
		mouseListen = new MouseListen(this);
	}
	
	public void render()
	{
		if(displayDB == null)
		{
			displayDB = new BufferedImage(display.getWidth(),display.getHeight(),BufferedImage.TYPE_INT_ARGB);
			osd = displayDB.getGraphics();
		}
		
		Graphics2D g2d = (Graphics2D)osd;
		g2d.setTransform(AffineTransform.getRotateInstance(0));
		
		osd.setColor(Color.WHITE);
		osd.fillRect(0,0,displayDB.getWidth(null),displayDB.getHeight(null));
		osd.setColor(Color.BLACK);

//		DRAW BOUNDING BOXES
//		for(Entity t : ents)
//		{
//			for(int x = 0; x < t.getBoundingBox().xpoints.length; x++)
//			{
//				int xloc[] = t.getBoundingBox().xpoints;
//				int yloc[] = t.getBoundingBox().ypoints;
//				
//				if(x < xloc.length-1)
//					osd.drawLine(xloc[x],yloc[x],xloc[x+1],yloc[x+1]);
//			}
//		}
	
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		
		for(Entity e : ents)
		{
			g2d.setTransform(AffineTransform.getRotateInstance(0));
	        
//	        if(e instanceof Bullet)
//	        {
//	        	g2d.drawLine((int)e.getX(),(int)e.getY(),(int)((Bullet)e).getEndX(),(int)((Bullet)e).getEndY());
//	        }
//	        else
	        {
	        	AffineTransform rotate = new AffineTransform();
				rotate.setToTranslation(e.getX(),e.getY());
//				DRAW HEALTH BARS
				g2d.setTransform(rotate);
				g2d.fillRect(-e.getImage().getWidth(null)/2,e.getImage().getHeight(null)/2+4,(int)(e.getImage().getWidth(null)*(e.getHealth()/e.getMaxHealth())),3);
				rotate.rotate(e.getAngle());
				g2d.setTransform(rotate);
				
				g2d.drawImage(e.getImage(),-e.getImage().getWidth(null)/2,-e.getImage().getHeight(null)/2,null);
	        }
		}
		
		display.getGraphics().drawImage(displayDB,0,0,null);
	}

	public void update()
	{
		removeEntities();
		
		if(player.isFiring())
			player.fire(ents);
		
		for(Entity t : ents)
		{
			t.move();
			t.recalculateBoundingBox();
		}
		
		for(Entity t : ents)
		{
			for(Entity p : ents)
				if(t != p)
					if(t.checkCollision(p.getBoundingBox()))
					{
						t.collide(p);
						p.collide(t);
					}
		}
	}
	
	public void enter()
	{
		
	}
	
	public void leave()
	{
		
	}
	
	public ArrayList<Entity> getEntities()
	{
		return ents;
	}
	
	public Player getPlayer()
	{
		return player;
	}
	
	private void removeEntities()
	{
		ArrayList<Entity> temp = new ArrayList<Entity>();
		
		for(int a = 0; a < ents.size(); a++)
			if(!ents.get(a).isMarked())
				temp.add(ents.get(a));
		
		ents = temp;
	}
}